﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileRenamer.GUI
{
    public partial class RtfToolTip : UserControl
    {
        private string m_rtfText;
        private Timer m_displayTimer = new Timer();
        private Timer m_hoverTimer = new Timer();
        private Size m_parentSize;
        private Size m_parentClientSize;
        private Rectangle m_ClientRectangle;
        private Point m_parentLocation;
        private Form m_parent;

        private int m_mouseMargin = 20;

        public int MouseMargin
        {
            get { return m_mouseMargin; }
            set { m_mouseMargin = value; }
        }

        private int m_textMargin = 4;
        public int TextMargin
        {
            get { return m_textMargin; }
            set { m_textMargin = value; }
        }

        public int m_hoverTime = 100;
        public int HoverTime
        {
            get
            {
                return m_hoverTime;
            }
            set
            {
                m_hoverTime = value;
            }
        }

        public string RtfText
        {
            get 
            { 
                return m_rtfText; 
            }
            set 
            {
                richTextBox1.Rtf = m_rtfText = value;
                SizeControl(richTextBox1.PreferredSize.Width, richTextBox1.PreferredSize.Height);
                richTextBox1.Location = new Point(m_textMargin, m_textMargin);
            }
        }

        private void SizeControl(int width, int height)
        {
            this.Size = new Size(width + (m_textMargin * 2), height + (m_textMargin * 2));
            richTextBox1.Size = new Size(width, height);
        }

        private bool m_view = false;
        public bool View
        {
            get
            {
                return m_view;
            }
            set
            {
                m_view = value;

                if (m_view == false)
                {
                    Visible = false;
                    m_hoverTimer.Stop();
                }
            }
        }

        private Point m_lastPosition;
        private Point m_position;

        public Point Position
        {
            get
            {
                return m_position;
            }
            set
            {
                // m_lastPosition = (m_position == null) ? value : m_position;
                m_position = value;
            }
        }

        public RtfToolTip(Form parent)
        {
            InitializeComponent();

            m_parent = parent;

            m_parent.Move += ParentMoved;
            m_parent.Resize += ParentMoved;

            m_displayTimer.Interval = 100;
            m_displayTimer.Tick += new EventHandler(m_displayTimer_Tick);
            m_displayTimer.Start();

            m_hoverTimer.Interval = 500;
            m_hoverTimer.Tick += new EventHandler(m_hoverTimer_Tick);
        }

        public void ParentMoved(object sender, EventArgs e)
        {
            Form parentForm = sender as Form;

            m_parentLocation = parentForm.Location;
            m_parentSize = parentForm.Size;
            m_parentClientSize = parentForm.ClientSize;
            m_ClientRectangle = parentForm.ClientRectangle;
        }

        void m_hoverTimer_Tick(object sender, EventArgs e)
        {
            int positionX;
            int positionY;

            // check the X axis
            if ((m_position.X + this.Size.Width + (2 * m_mouseMargin)) > m_parentClientSize.Width)
            {
                positionX = m_parentClientSize.Width - this.Size.Width - m_mouseMargin;
            }
            else
            {
                positionX = m_position.X + m_mouseMargin;
            }

            // check the Y axis
            if ((m_position.Y + this.Size.Height + (2 * m_mouseMargin)) > m_parentClientSize.Height)
            {
                positionY = m_parentClientSize.Height - this.Size.Height - m_mouseMargin;
            }
            else
            {
                positionY = m_position.Y + m_mouseMargin;
            }

            // TODO we need to have an algorithm that moves it to one of the four quadrants round the mouse
            if (positionY < (m_position.Y + m_mouseMargin))
            {
                if (m_position.X > (m_parentClientSize.Width / 2))
                {
                    positionX = m_position.X - this.Size.Width - m_mouseMargin;
                }
                else
                {
                }
                positionY = m_parentClientSize.Height - this.Size.Height - m_mouseMargin;
            }

            Location = new Point(positionX, positionY);

            if (!string.IsNullOrWhiteSpace(richTextBox1.Text))
            {
                Visible = true;
            }

            m_hoverTimer.Stop();
        }

        void m_displayTimer_Tick(object sender, EventArgs e)
        {
            if (m_view == true)
            {
                if (m_lastPosition == m_position)
                {
                    m_hoverTimer.Start();
                }
                else
                {
                    m_lastPosition = Position;
                    m_hoverTimer.Stop();
                    Visible = false;
                }
            }
            else
            {
                Visible = false;
            }
        }
    }
}
